import {
  Controller,
  Post,
  Body,
  Get,
  Res,
  Headers,
  Req,
  // Req,
  // UseInterceptors,
  // UploadedFile,
} from '@nestjs/common';
import { FastifyRequest } from 'fastify';
import {
  ApiOperation, //接口添加描述信息
  ApiTags, //接口添加分组名字
  ApiParam, //接口参数描述
  // ApiQuery, //query参数描述
} from '@nestjs/swagger';
import { HttpOrderService } from './httpOrder.service';
// import { ConfigService } from '@nestjs/config';
// import { HttpOrder } from './httpOrder.entity';
import { Authorize } from '../modules/admin/core/decorators/authorize.decorator';
import { Keep } from '@main/common/decorators/keep.decorator';
import { Response } from 'express';
// import { FileInterceptor } from '@nestjs/platform-express';
// import multer from 'multer';
// import { join } from 'path';
// import * as path from 'path';
// import { app } from 'electron';
// import { FastifyRequest } from 'fastify';

@Controller('http/produce')
@ApiTags('HttpOrder')
export class HttpOrderController {
  constructor(private readonly httpOrderService: HttpOrderService) {}

  @Keep()
  @Post('order/send')
  @Authorize()
  @ApiOperation({
    summary: 'erp生产工单接口',
    description: 'erp生产工单接口',
  })
  @ApiParam({
    name: 'command',
    description: '命令',
    required: true,
    type: Number,
  })
  @ApiParam({
    name: 'machineCode',
    description: '设备编码',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'proOrder',
    description: '开工顺序',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'customerCode',
    description: '客户编号',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'customerName',
    description: '客户名称',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'customerBoxModel',
    description: '客户盒式',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'paperTexture',
    description: '纸(材)质',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'corrugatedTypeId',
    description: '楞别',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'materialLength',
    description: '长',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'materialWidth',
    description: '宽',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'materialHeight',
    description: '高',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'unit',
    description: '单位',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'cartonTypeId',
    description: '箱型',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'nail',
    description: '钉口',
    required: false,
    type: String,
  })
  @ApiParam({ name: 'lid', description: '摇盖', required: false, type: String })
  @ApiParam({
    name: 'orderNum',
    description: '工单数量(成品)',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'paperboardLength',
    description: '纸板长度',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'paperboardWidth',
    description: '纸板宽度',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'paperboardNum',
    description: '纸板张数',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'templateNum',
    description: '模数',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'orderTypeId',
    description: '工单类型',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'materialCode',
    description: '产品编号',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'materialName',
    description: '产品名称',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'materialPicture',
    description: '产品图片文件名称',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'customerDeliveryDate',
    description: '客户交期',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'erpNo',
    description: 'erp工单号',
    required: true,
    type: String,
  })
  async sendOrder(
    @Body() order: any,
    @Headers('language') languageType,
    @Req() req: FastifyRequest
  ): Promise<{ status: string; message: string; data: any }> {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;

    const result = await this.httpOrderService.validateOrder(
      order,
      languageType,
      mac,
      ip,
      syncHostSqlite
    );

    if (result.status === 'E') {
      return { status: 'E', message: result.message, data: null };
    }
    return { status: 'S', message: null, data: null };
  }

  @Keep()
  @Post('report/upload')
  @Authorize()
  @ApiOperation({
    summary: 'erp生产工单接口',
    description: 'erp生产工单接口',
  })
  @ApiParam({
    name: 'date',
    description: '日期',
    required: true,
    type: Number,
  })
  @ApiParam({
    name: 'machineCode',
    description: '设备标识',
    required: false,
    type: String,
  })
  @ApiParam({
    name: 'erpNo',
    description: '第三方订单号',
    required: false,
    type: String,
  })
  async findByDateAndProOrder(
    @Body() body: any
  ): Promise<{ status: string; message: string; data: any }> {
    const result = await this.httpOrderService.findByDateAndProOrder(body);
    if (result.status === 'E') {
      return { status: 'E', message: result.message, data: null };
    }
    return { status: 'S', message: null, data: result.data };
  }

  @Keep()
  @Authorize()
  @Get('getSqlite')
  async getSqlite(@Res() res: Response) {
    // 创建文件流并将其发送到客户端

    res.header('Content-Type', 'application/octet-stream');

    res.header('Content-Disposition', 'attachment; filename=sqlite.zip');

    res.send(await this.httpOrderService.getSqlite());
  }

  @Keep()
  @Authorize()
  @Get('getUploadFile')
  async getUploadFile(@Res() res: Response) {
    // 创建文件流并将其发送到客户端

    res.header('Content-Type', 'application/octet-stream');

    res.header('Content-Disposition', 'attachment; filename=sqlite.zip');

    res.send(await this.httpOrderService.getUploadFile());
  }

  @Keep()
  @Authorize()
  @Post('syncUploadFile')
  async syncUploadFile(@Body() dto) {
    // console.log(dto);
    const data = dto['file'][0].data;

    await this.httpOrderService.syncUploadFile(data);
    // const files = await req.files; // 获取上传的文件对象

    // console.log(files);
    // const parts = req.parts();
    // console.log(req.data)
    // console.log(parts);
    // for await (const part of parts) {
    //   console.log(part)
    //   if (part.file) {
    //     // 获取上传文件的流数据
    //     const fileStream = part.file;
    //     await this.httpOrderService.syncUploadFile(dto);
    //   }
    // }
  }

  @Keep()
  @Authorize()
  @Post('syncSqlite')
  async syncSqlite(@Body() list: any) {
    this.httpOrderService.syncSqlite(list);
  }
}
